/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farent.solutions.common.discovery;

import com.farent.solutions.common.discovery.ClassResolver.ClassSelectionStrategy;



/**
 * A Test that checks to see if each class has registered annotation. Note
 * that this test will match the parent type itself if it is presented for matching.
 * @author Farouk.Alhassan
 */
public class HasAnnotationSelectionStrategy implements ClassSelectionStrategy {

    private Class<?> annotationClzz;

    /** Constructs an HasAnnotation test using the supplied Class as the parent class/interface. */
    public HasAnnotationSelectionStrategy(Class<?> annoType) {
        this.annotationClzz = annoType;
    }

    public void setAnnotationClzz(Class<?> annotationClzz) {
        this.annotationClzz = annotationClzz;
    }

    public HasAnnotationSelectionStrategy() {
    }

    /** Returns true if type is assignable to the parent type supplied in the constructor. */
    @SuppressWarnings("unchecked")
    @Override
    public boolean matches(Class type) {
        boolean ans = type != null && type.isAnnotationPresent(annotationClzz);
        return ans;
    }

    @Override
    public String toString() {
        return "is assignable to " + annotationClzz.getSimpleName();
    }
    
    @Override
    public void setClass(Class<?> type) {
        this.annotationClzz = type;
    }
}
